package service

import (
	"context"
	"rpc_server/basic/config"
	__ "rpc_server/basic/proto"
	"rpc_server/handler/model"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedRpcServerServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) SmsMessage(_ context.Context, in *__.SmsMessageReq) (*__.SmsMessageResp, error) {
	user := model.User{
		Mobile: in.Mobile,
		Code:   uint(in.Code),
	}
	err := config.DB.Create(&user).Error
	if err != nil {
		return &__.SmsMessageResp{
			Coe: 500,
			Msg: "failed",
		}, err
	}
	return &__.SmsMessageResp{
		Coe: 200,
		Msg: "success",
	}, nil
}
func (s *Server) Register(_ context.Context, in *__.RegisterReq) (*__.RegisterResp, error) {
	user := model.User{
		Username: in.Username,
		Password: in.Password,
	}
	err := config.DB.Create(&user).Error
	if err != nil {
		return &__.RegisterResp{
			Coe: 500,
			Msg: "failed",
		}, err
	}
	return &__.RegisterResp{
		Coe: 200,
		Msg: "success",
	}, nil
}
func (s *Server) Login(_ context.Context, in *__.LoginReq) (*__.LoginResp, error) {
	user := model.User{
		Username: in.Username,
		Password: in.Password,
	}

	err := config.DB.Where("username = ? AND password = ?", user.Username, user.Password).Find(&user).Error
	if err != nil {
		return &__.LoginResp{
			Coe: 500,
			Msg: "failed",
		}, err
	}
	return &__.LoginResp{
		Coe: 200,
		Msg: "success",
	}, nil
}
